<template>
  <div>
    <el-form ref="form" label-width="80px">
      <el-form-item label="Spu名称">
        {{ spu.spuName }}
      </el-form-item>
      <el-form-item label="Sku名称">
        <el-input placeholder="Sku名称" v-model="skuInfo.skuName"></el-input>
      </el-form-item>
      <el-form-item label="价格(元)">
        <el-input placeholder="价格(元)" type="number" v-model.number="skuInfo.price"></el-input>
      </el-form-item>
      <el-form-item label="重量(千克)">
        <el-input placeholder="重量(千克)" type="number" v-model.number="skuInfo.weight"></el-input>
      </el-form-item>
      <el-form-item label="规格描述">
        <el-input type="textarea" placeholder="规格描述" rows="4" v-model="skuInfo.skuDesc"></el-input>
      </el-form-item>
      <el-form-item label="平台属性">
        <el-form :inline="true" ref="form" label-width="80px">
          <el-form-item :label="attr.attrName" v-for="attr in attrInfoList" :key="attr.id">
            <el-select placeholder="请选择" v-model="attr.attrAndValueId">
              <el-option :label="attrValue.valueName" :value="`${attr.id}:${attrValue.id}`" v-for="attrValue in attr.attrValueList" :key="attrValue.id"></el-option>
            </el-select>
          </el-form-item>
        </el-form>
      </el-form-item>
      <el-form-item label="销售属性">
        <el-form :inline="true" ref="form" label-width="80px">
          <el-form-item :label="saleAttr.saleAttrName" v-for="saleAttr in spuSaleAttrList" :key="saleAttr.id">
            <el-select placeholder="请选择" v-model="saleAttr.attrAndValueId">
              <el-option :label="saleAttrValue.saleAttrValueName" :value="`${saleAttr.id}:${saleAttrValue.id}`" v-for="saleAttrValue in saleAttr.spuSaleAttrValueList" :key="saleAttrValue.id"></el-option>
            </el-select>
          </el-form-item>
        </el-form>
      </el-form-item>
      <el-form-item label="图片列表">
        <el-table style="width: 100%" border :data="spuImageList">
          <el-table-column type="selection" width="width" @selection-change="handleSelectionChange"> </el-table-column>
          <el-table-column prop="prop" label="图片" width="300px">
            <template slot-scope="{ row }">
              <img :src="row.imgUrl" alt="" style="width: 50%" />
            </template>
          </el-table-column>
          <el-table-column prop="imgName" label="名称" width="width"> </el-table-column>
          <el-table-column prop="prop" label="操作" width="width">
            <template slot-scope="{ row }">
              <el-button type="primary" v-if="row.isDefault == 0" @click="setDefault(row)">设置默认</el-button>
              <el-tag type="success" v-else>默认</el-tag>
            </template>
          </el-table-column>
        </el-table>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="saveSkuInfo">保存</el-button>
        <el-button @click="cancel">取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  name: 'SkuForm',
  data() {
    return {
      spuImageList: [],
      spuSaleAttrList: [],
      // 平台属性
      attrInfoList: [],
      // 收集skuForm数据
      skuInfo: {
        category3Id: '',
        tmId: '',
        spuId: '',
        skuName: '',
        price: 0,
        weight: 0,
        skuDesc: '',
        skuDefaultImg: '',
        skuImageList: [],
        skuAttrValueList: [],
        skuSaleAttrValueList: [],
      },
      spu: {},
      // 存储缺少isDefault属性的图片列表
      imageList: [],
    }
  },
  methods: {
    async getSkuForm(category1Id, category2Id, spu) {
      this.skuInfo.category3Id = spu.category3Id
      this.skuInfo.tmId = spu.tmId
      this.skuInfo.spuId = spu.id
      this.spu = spu
      let res = await this.$API.Spu.reqSpuImageList(spu.id)
      if (res.code == 200) {
        let list = res.data
        list.forEach((item) => {
          item.isDefault = 0
        })
        this.spuImageList = list
      }
      let res2 = await this.$API.Spu.reqSpuSaleAttrList(spu.id)
      if (res2.code == 200) {
        this.spuSaleAttrList = res2.data
      }
      let res3 = await this.$API.Spu.reqAttrInfoList(category1Id, category2Id, spu.id)
      if (res3.code == 200) {
        this.attrInfoList = res3.data
      }
    },
    handleSelectionChange(value) {
      this.imageList = value
    },
    setDefault(row) {
      this.spuImageList.forEach((item) => {
        item.isDefault = 0
      })
      row.isDefault = 1
      this.skuInfo.skuDefaultImg = row.imgUrl
    },
    async saveSkuInfo() {
      // 清空上一次残留的数据
      Object.assign(this._data, this.$options.data())
      // 解构赋值
      const { attrInfoList, spuSaleAttrList, imageList } = this
      // 整理平台属性参数
      this.skuInfo.skuAttrValueList = attrInfoList.reduce((pre, cur) => {
        if (cur.attrAndValueId) {
          const [attrId, valueId] = cur.attrAndValueId.split(':')
          pre.push({ attrId, valueId })
        }
        return pre
      }, [])
      // 整理销售属性参数
      this.skuInfo.skuSaleAttrValueList = spuSaleAttrList.reduce((pre, cur) => {
        if (cur.attrAndValueId) {
          const [saleAttrId, saleAttrValueId] = cur.attrAndValueId.split(':')
          pre.push({ saleAttrId, saleAttrValueId })
        }
        return pre
      }, [])
      // 整理图片列表参数
      this.skuInfo.skuImageList = imageList.map((item) => {
        return {
          imgUrl: item.imgUrl,
          imgName: item.imgName,
          isDefault: item.isDefault,
          spuImgId: item.id,
        }
      })
      let res = await this.$API.Spu.reqAddSku(this.skuInfo)
      if (res.code == 200) {
        this.$message({ type: 'success', message: '保存成功！' })
        this.$emit('changeScene', { scene: 0 })
      }
    },
    cancel() {
      this.$emit('changeScene', { scene: 0 })
      // 清空上一次残留的数据
      Object.assign(this._data, this.$options.data())
    },
  },
}
</script>

<style>
</style>